<?php
namespace spider\Controller;
use Think\Controller;
use QL\QueryList;
use Sunra\PhpSimple\HtmlDomParser;
set_time_limit(0);
error_reporting(0);

class HuomaoController extends Controller {

    public function index(){
        $star_time=time();
        //从配置文件中获得火猫平台需要爬取的直播类型
        $game_type = C('HUOMAO_GAME');
        //设置起始页
        $page = 1;
        //在线人数作为是否继续爬取的判断条件
        $top_views = 100;
        /*设置一个开关，当第一次更新直播表的时候，将所有记录中的isOnlive设置为0，然后在进行更新,
                 接下来更新的时候就不用更新isOnlive字段了
               */
        $is_update = true;
        //循环，相当于对每一页进行解析，
        while ($page < 5 ){
            //sleep(2);
            //$top_views = 90;

            $huomao_api = sprintf(C("HUOMAO_API"),$page);
            echo $huomao_api;
            //获得api中的json
            $response = QueryList::Query($huomao_api,array('html'=>'html'))->html;
            //echo $huomao_api."<br/>";

            //如果能够从api中获得到json则进行下一步操作
            if($response && strlen($response) != 0){
                $res_arr = json_decode($response,true);
                $res_list = $res_arr['data']['channelList'];


                //获得单个直播接相关信息
                foreach ($res_list as $k=> $v){
                    //判断是否在直播
                    if($v['is_live'] == '0'){
                        continue;
                    }
                    $gameHostName = $v['gid'];
                    //如果爬取的直播类型不在设定的类型中，则跳过
                    if(!array_key_exists($gameHostName,$game_type)){
                        continue;
                    }else{
                       //如果点击量中有‘万’，则去掉万，并计算整数
                       if(strpos($v['views'],'万')){
                           $views = floatval(substr($v['views'],0,-1))*10000;
                       //
                       }elseif (strpos($v['views'],',')){
                           $views = str_replace(',','',$v['views']);
                       }else{
                           $views = $v['views'];
                       }
                    }
                    //当采集到的直播观看人数少于设定的值，则
                    if($top_views > $views){
                        continue;
                    }
                    $kw['title'] = $v['channel'];
                    $kw['thumb'] = $v['image'];
                    $kw['keywords'] ='';
                    $kw['description'] = '';
                    $kw['inputtime'] = time();
                    $kw['updatetime'] = time();
                    $kw['zb_thumb'] = $this->views_asynchronous($v['id']);
                    $kw['zbid'] = $v['id'];
                    $kw['zbname'] = $v['nickname'];
                    $kw['source'] = 'huomao';
                    $kw['category'] = $game_type[$gameHostName];
                    $kw['isOnlive'] = '1';
                    $kw['livedata'] = $this->get_zb_room_str($v['id']);
                    $kw['m_livedata'] = '';
                    $kw['views'] = $views;
                    //$this->update_huomao('huomao',$kw);
                    if($is_update){
                        befor_update('huomao');
                        update_zhibo('huomao',$kw);
                        $is_update=false;
                    }else{
                        update_zhibo('huomao',$kw);
                    }
                }
            }
            //下一页
            $page+=1;
        }
        echo 'time cost '.(time()-$star_time).' seconds';

    }
    private function views_asynchronous($id){

        //$id=4589;
        $url = sprintf(C('HUOMAO_VIEWS'),$id);
//        $view_html = QueryList::Query($url,array('zb_thumb'=>array('.top-leftbox img'=>'src')))->html;
        //$html_ob = new HtmlDomParser();
        $html = HtmlDomParser::file_get_html($url);
        if($html==false){
            $zb_thumb='';
        }else{
            $zb_thumb_arr = $html->find('.top-leftbox img','0')->attr;
            $zb_thumb = $zb_thumb_arr['src'];
            $html->clear();
        }

        return $zb_thumb;


//        $view_html = QueryList::Query($url,array('zb_thumb'=>array('.top-leftbox'=>'src')));
//        $data = $view_html->getData(function ($item){
//            return $item;
//        });
//        print_r($data);


    }

    private function get_zb_room_str($id){
        //获得页面的html代码
        $url = sprintf(C('HUOMAO_VIEWS'),$id);
        $view_html = QueryList::Query($url,array('html'=>'html'))->html;
        preg_match('/getFlash\("\d+","(.+)"\)/',$view_html,$result);
        return 'http://static.huomao.com/swf/huomaoplayer.swf?ver=20150721&initSize=big&VideoIDS='.$result[1].'&live_id='.$id.'&streamtype=live&autostart=true&url=http://www.huomaotv.com&s=swf&g=get_video_data&hasDanmu=0&hasFitPage=0&hasLowBitrate=1&hasFeedBack=0&hasRefresh=1';
    }

}